/*
*	Copyright (C) 2014  Julio Montes, Mario Carrillo
*	
*	Julio Montes e-mail: imc.coder@gmail.com 
*
*	Mario Carrillo e-mail: mario_90ago@live.com.mx
*
*	BSD 3-Clause License
*/

#include "alliance/include/alliancemenu.h"

#include "alliance/include/projectmgr.h"
#include "alliance/include/path.h"
#include "alliance/include/toolpanel.h"
#include "alliance/include/viewerdialog.h"

#include "sdk/include/cfgmgr.h"
#include "sdk/include/filecfg.h"
#include "sdk/include/path.h"

#include <wx/wx.h>
#include <wx/dir.h>

#include "include/iaManager.h"
#include "include/iaMainFrame.h"

#define ID_NEW_SOURCE_FILE 1001
#define ID_ADD_FILES 1002
#define ID_CREATE_PROJECT 1003
#define ID_OPEN_PROJECT 1004
#define ID_SAVE_PROJECT 1005
#define ID_SAVE_ALL_PROJECTS 1006
#define ID_CLOSE_PROJECT 1007
#define ID_CLOSE_ALL_PROJECTS 1008
#define ID_BUILD 1011
#define ID_BUILD_OPTIONS 1012
#define ID_VIEWERS 1030 //this must be the last


#define ID_PAT_FILE 1009
#define ID_IOC_FILE 1010


namespace alliance
{
    AllianceMenu::AllianceMenu()
        : wxMenu()
    {
        m_subMenuFileAlliance = this;
        wxMenuBar *menubar = iaManager::Get()->GetMainFrame()->GetMenuBar();
        const int indexFileMenu = menubar->FindMenu( wxT( "File" ) );
        if( indexFileMenu != wxNOT_FOUND )
        {
            wxMenu *fileMenu = menubar->GetMenu( indexFileMenu );
            m_subMenuFileAlliance = new wxMenu();
            wxMenuItem *menuItem = new wxMenuItem( fileMenu, wxID_ANY, wxT( "alliance" ), wxEmptyString, wxITEM_NORMAL, m_subMenuFileAlliance );

            int position = fileMenu->FindItem( wxT( "Open file" ) );
            if( wxNOT_FOUND != position )
            {
                int temp = position;
                fileMenu->FindChildItem( temp, (size_t *)&position );
                ++position;
            }
            else
            {
                position = 3;
            }

            fileMenu->InsertSeparator( position++ );
            fileMenu->Insert( position, menuItem );
        }

        m_subMenuFileAlliance->Append( ID_NEW_SOURCE_FILE, wxT( "&New source file" ) );
        m_subMenuFileAlliance->Append( ID_ADD_FILES, wxT( "&Add files" ) );
        m_subMenuFileAlliance->AppendSeparator();
        m_subMenuFileAlliance->Append( ID_CREATE_PROJECT, wxT( "&Create project" ) );
        m_subMenuFileAlliance->Append( ID_OPEN_PROJECT, wxT( "&Open project" ) );
        m_subMenuFileAlliance->AppendSeparator();
        m_subMenuFileAlliance->Append( ID_SAVE_PROJECT, wxT( "&Save project" ) );
        m_subMenuFileAlliance->Append( ID_SAVE_ALL_PROJECTS, wxT( "Save all projects" ) );
        m_subMenuFileAlliance->AppendSeparator();
        m_subMenuFileAlliance->Append( ID_CLOSE_PROJECT, wxT( "Close project" ) );
        m_subMenuFileAlliance->Append( ID_CLOSE_ALL_PROJECTS, wxT( "Close all projects" ) );


        m_subMenuFileAlliance->AppendSeparator();
        m_subMenuFileAlliance->Append( ID_PAT_FILE, wxT( "New pat file" ) );
        m_subMenuFileAlliance->Append( ID_IOC_FILE, wxT( "New ioc file" ) );


        Append( ID_BUILD, wxT( "Build\tCtrl+F9" ) );
        AppendSeparator();
        Append( ID_BUILD_OPTIONS, wxT( "Build options" ) );


        //TOOLBAR
        m_toolbar = new wxToolBar( iaManager::Get()->GetMainFrame(), wxID_ANY );
        m_toolbar->AddTool( ID_BUILD, wxT( "" ), wxBitmap( Path::s_dirImages + wxT( "build.png" ), wxBITMAP_TYPE_PNG ), wxT( "Build" ) );
        m_toolbar->AddSeparator();

        wxDir::GetAllFiles( Path::s_dirViewers, &m_arrayViewers, _T( "*.viewer" ), wxDIR_FILES );
        for( size_t i=0; i<m_arrayViewers.Count(); ++i )
        {
            const wxString &viewerName = m_arrayViewers[i].AfterLast( _T( '/' ) ).BeforeLast( _T( '.' ) );
            const wxString &viewerImage = Path::s_dirImages + viewerName + _T( ".png" );
            if( wxFileExists( viewerImage ) )
            {
                m_toolbar->AddTool( ID_VIEWERS + i, wxT( "" ), wxBitmap( viewerImage, wxBITMAP_TYPE_PNG ), viewerName );
            }
            else
            {
                m_toolbar->AddTool( ID_VIEWERS + i, viewerName, wxBitmap( Path::s_dirImages + _T( "viewer.png" ), wxBITMAP_TYPE_PNG ), viewerName );
            }

            iaManager::Get()->GetMainFrame()->Connect( ID_VIEWERS + i, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AllianceMenu::OnViewer ), NULL, this );

        }

        sdk::FileConfig *fcLayout = sdk::ConfigManager::Get()->GetFile( sdk::Path::s_dirConfig + wxT( "settings_layout.xml" ) );
        m_auiMgr = iaManager::Get()->GetMainFrame()->GetAuiManager();
        m_auiMgr->InsertPane( m_toolbar, wxAuiPaneInfo().ToolbarPane().Top().Floatable( false ).Position( 1 ) );
        m_auiMgr->LoadPaneInfo( fcLayout->ReadString( wxT( "/perspective/toolbar/alliance" ), wxT( "" ) ), m_auiMgr->GetPane( m_toolbar ) );
        m_auiMgr->Update();



        iaManager::Get()->GetMainFrame()->Connect( ID_NEW_SOURCE_FILE, ID_BUILD_OPTIONS, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler( AllianceMenu::OnMenu ), NULL, this );
        iaManager::Get()->GetMainFrame()->Connect( ID_NEW_SOURCE_FILE, ID_BUILD_OPTIONS, wxEVT_UPDATE_UI, wxUpdateUIEventHandler( AllianceMenu::OnUpdateUI ), NULL, this );

    }

    AllianceMenu::~AllianceMenu()
    {

    }

    void AllianceMenu::OnMenu( wxCommandEvent &evt )
    {

        switch( evt.GetId() )
        {
        case ID_NEW_SOURCE_FILE:
            ProjectManager::Get()->NewSourceFile();
            break;

        case ID_ADD_FILES:
            ProjectManager::Get()->AddFiles();
            break;

        case ID_CREATE_PROJECT:
            ProjectManager::Get()->CreateProject();
            break;

        case ID_OPEN_PROJECT:
            ProjectManager::Get()->OpenProject();
            break;

        case ID_SAVE_PROJECT:
            ProjectManager::Get()->SaveActiveProject();
            break;

        case ID_SAVE_ALL_PROJECTS:
            ProjectManager::Get()->SaveAllProjects();
            break;

        case ID_CLOSE_PROJECT:
            ProjectManager::Get()->CloseActiveProject();
            break;

        case ID_CLOSE_ALL_PROJECTS:
            ProjectManager::Get()->CloseAllProjects();
            break;

        case ID_BUILD:
            ProjectManager::Get()->Build();
            break;

        case ID_BUILD_OPTIONS:
            ProjectManager::Get()->BuildOptions();
            break;

        case ID_PAT_FILE:
            ProjectManager::Get()->NewPatFile();
            break;

        case ID_IOC_FILE:
            ProjectManager::Get()->NewIocFile();
            break;
        }
    }

    void AllianceMenu::OnViewer( wxCommandEvent &evt )
    {
        const wxString &viewerFile = m_arrayViewers[evt.GetId()-ID_VIEWERS];
        const wxString &viewerName = viewerFile.AfterLast( _T( '/' ) ).BeforeLast( _T( '.' ) );
        if( wxFileExists( viewerFile ) )
        {
            ViewerDialog dlg( NULL, viewerName, viewerFile );
            dlg.ShowModal();
        }
    }


    void AllianceMenu::OnUpdateUI( wxUpdateUIEvent &evt )
    {
        Project *project = ProjectManager::Get()->GetActiveProject();
        const bool newSourceFile = ( project != NULL );
        const bool newPatFile = ( project != NULL );
        const bool newIocFile = ( project != NULL );

        const bool addFiles = ( project != NULL );
        const bool saveProject = ( project != NULL && project->IsModified() );
        const bool saveAllProjects = ( project != NULL );
        const bool closeProject = ( project != NULL && !project->IsBusy() );
        const bool closeAllProjects = ( project != NULL && !project->IsBusy() );
        const bool build = ( project != NULL );
        const bool viewer = ( project != NULL );
        const bool buildOptions = ( project != NULL && !project->IsBusy() );

        m_subMenuFileAlliance->Enable( ID_NEW_SOURCE_FILE, newSourceFile );

        m_subMenuFileAlliance->Enable( ID_PAT_FILE, newPatFile );
        m_subMenuFileAlliance->Enable( ID_IOC_FILE, newIocFile );

        m_subMenuFileAlliance->Enable( ID_ADD_FILES, addFiles );
        m_subMenuFileAlliance->Enable( ID_SAVE_PROJECT, saveProject );
        m_subMenuFileAlliance->Enable( ID_SAVE_ALL_PROJECTS, saveAllProjects );
        m_subMenuFileAlliance->Enable( ID_CLOSE_PROJECT, closeProject );
        m_subMenuFileAlliance->Enable( ID_CLOSE_ALL_PROJECTS, closeAllProjects );

        Enable( ID_BUILD, build );
        Enable( ID_BUILD_OPTIONS, buildOptions );

        if( m_auiMgr->GetPane( m_toolbar ).IsShown() )
        {
            m_toolbar->EnableTool( ID_BUILD, build );
            for( size_t i=0; i<m_arrayViewers.Count(); ++i )
            {
                m_toolbar->EnableTool( ID_VIEWERS + i, viewer );
            }
        }
    }
}
